import lib.problem as problem

#Todo: improvement: store the length for each number
# whenever an already stored number appears we don't have 
# to calculate it again

class Problem014(problem.Problem):
	def __init__(self):
		problem.Problem.__init__(self, 14)
		
	
	def oddStep(self, n):
		return 3 * n + 1
				
	def evenStep(self, n):
		return n / 2
			
	
	def collatzChain(self, n):
		c = n
		result = [c]
		
		while not c == 1:
			if c % 2:
				c = self.oddStep(c)
			else:
				c = self.evenStep(c)
				
				result.append(c)
		return result

	
	def solve(self):
		
		maxChainLen = 0
		for i in range(1, 1000000):
			chainLen = len(self.collatzChain(i))
			if chainLen > maxChainLen:
				maxChainLen = chainLen
		
		return i 

	
if __name__ == "__main__":
	print(Problem014().solveAndTime())
	